package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import vo.Commodity;
import vo.Store;
import dao.GoodsDao;
@Repository("gd")
public class GoodsDaoImpl implements GoodsDao{
	@PersistenceContext(name="un")
	private EntityManager em;
	@Override
	public int add(Commodity goods) {
		em.persist(goods);
		
		return goods.getCommodityId();
	}
	@Override
	public void deleteGoods(int ids){
		String sqle="delete from Commodity sp where sp.commodityId=:ids";
		em.createQuery(sqle).setParameter("ids",ids).executeUpdate();
	}
	@Override
	public List<Commodity> updateGoods(Commodity goods) {
		String sql="update Commodity sp set sp.commodityCode=:commodityCode,"
				+ "sp.commodityName=:commodityName,sp.commodityPicture=:commodityPicture,"
				+ "sp.commodityPrice=:commodityPrice,sp.commodityType=:commodityType,"
				+ "sp.commodityDiscount=:commodityDiscount,sp.commodiryNum=:commodiryNum,"
				+ "sp.store=:store,sp.commodityTip=:commodityTip "
				+ "where sp.commodityId=:commodityId";
		em.createQuery(sql)
				.setParameter("commodityCode",goods.getCommodityCode())
				.setParameter("commodityName",goods.getCommodityName())
				.setParameter("commodityPicture",goods.getCommodityPicture())
				.setParameter("commodityPrice",goods.getCommodityPrice())
				.setParameter("commodityType",goods.getCommodityType())
				.setParameter("commodityDiscount",goods.getCommodityDiscount())
				.setParameter("commodiryNum",goods.getCommodiryNum())
				.setParameter("store",goods.getStore())
				.setParameter("commodityTip",goods.getCommodityTip())
				.setParameter("commodityId",goods.getCommodityId())
				.executeUpdate();
				//.getResultList();
		
		return null;
	}
	public List<Commodity> queryAllGoods(Store store) {
		String sql="select gs from Commodity gs where gs.store=:store";
		String sql1="select gs from Commodity gs ";
		List<Commodity> cds=em.createQuery(sql)
				.setParameter("store",store)
				.getResultList();
		if(!cds.isEmpty()){
			return cds;
		}
		else{
			return null;
		}
		
	}
	public Commodity queryGoods(int id) {
		String sql="select gs from Commodity gs where gs.commodityId=:commodityId";
		
		List<Commodity> cds=em.createQuery(sql)
				.setParameter("commodityId",id)
				.getResultList();
		System.out.println("==========");
		
		if(!cds.isEmpty()){
			System.out.println(cds.get(0).getCommodityId());
			return cds.get(0);
			
		}
		else{
			return null;
		}
		
	}
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     	

}
